﻿package com.powerunion.extjs.utils;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.newsoft.util.vo.TreeParaVo;

/**
 * <p>
 * Title: 农合
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company: newsoft
 * </p>
 * 
 * @author davi
 * @version 1.0
 */
public class Instance {
	private String className;

	// 返回业务对象实例
	public static Object create(String className) {
		Class clazz = null;
		try {
			if (className.indexOf("ComboboxUtils") >= 0) {
				clazz = Class.forName("com.newsoft.util." + className);
			} else {
				clazz = Class.forName(getClassPath(className));
			}
			// System.err.println("clazz:"+clazz);
			return clazz.newInstance();
		} catch (ClassNotFoundException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return null;
	}

	// 从dwr配置文件中读取业务对象路径
	public static String getClassPath(String className) {
		String configFileName = Path.getFullPathRelateClass("../../../..",
				Instance.class)
				+ "/dwr.xml";
		Document doc = getXml(configFileName);
		String value = null;
		Element firstEl = (Element) doc.getElementsByTagName("allow").item(0);
		NodeList list = firstEl.getElementsByTagName("create");
		for (int i = 0; i < list.getLength(); i++) {
			Element el = (Element) list.item(i);
			String attribute = el.getAttribute("javascript");
			if (attribute.indexOf(className) == 0
					&& attribute.length() == className.length()) {
				value = el.getAttribute("class");
				// System.err.println("value:"+value);
				break;
			}
		}
		return value;
	}

	// 从ComboboxUtils.xml文件中读取[通用查询下拉数据框]查询语句
	public static String getQueryHql(String tagName) {
		String value = null;
		String configFileName = Path.getFullPathRelateClass("", Instance.class)
				+ "/ComboboxUtils.xml";
		Document doc = getXml(configFileName);
		Element el = (Element) doc.getElementsByTagName(tagName).item(0);
		try {
			value = el.getFirstChild().getNodeValue();
		} catch (Exception e) {
			System.err.println("从[ComboboxUtils.xml]读取查询语句[" + tagName + "]错误");
			value = null;
		}

		return value;
	}

	// 从ComboboxUtils.xml文件中读取树的查询语句
	// 若节点含有flag属性,按照新的格式解析xml文件,并返回参数值对像.
	// 否则按照原有方法返回字符串.
	public static Object getTreeHql(String tagName) {
		String value = null;
		String configFileName = Path.getFullPathRelateClass("", Instance.class)
				+ "/ComboboxUtils.xml";
		Document doc = getXml(configFileName);
		Element el = (Element) doc.getElementsByTagName(tagName).item(0);
		String flag = el.getAttribute("flag");
		if (null == flag || "".equals(flag)) {
			try {
				value = el.getFirstChild().getNodeValue();
			} catch (Exception e) {
				System.err.println("从[ComboboxUtils.xml]读取查询语句[" + tagName
						+ "]错误");
				value = null;
			}
			return value;
		} else {
			if (!"sql".equals(flag)) {
				flag = "hql";
			}
			TreeParaVo vo = new TreeParaVo();
			vo.setFlag(flag);
			try {
				for (Node node = el.getFirstChild(); node != null; node = node
						.getNextSibling()) {
					if (node.getNodeType() == Node.ELEMENT_NODE) {
						if (node.getNodeName().equals("sql")) {
							vo.setSql(node.getFirstChild().getNodeValue());
						} else if (node.getNodeName().equals("search_sql")) {
							vo.setSearch_sql(node.getFirstChild()
									.getNodeValue());
						}
					}
				}
			} catch (Exception e) {
				System.err.println("从[ComboboxUtils.xml]读取查询语句[" + tagName
						+ "]错误,带有flag查询属性.");
				vo = null;
			}
			return vo;
		}
	}

	// 从list.xml文件中读取list方法的sql语句（在当前目录下）
	// 若节点含有flag属性,按照新的格式解析xml文件,并返回参数值对像.
	// 否则按照原有方法返回字符串.
	public static TreeParaVo getListHql(String classname, String path) {
		Document doc = getXml(path);
		Element el = (Element) doc.getElementsByTagName(classname).item(0);
		TreeParaVo vo = new TreeParaVo();
		try {
			vo.setFlag(el.getAttribute("flag"));
			vo.setSql(el.getFirstChild().getNodeValue());
		} catch (Exception e) {
			System.err.println("从[" + path + "]读取list语句[" + classname + "]错误");
			vo = null;
			e.printStackTrace();
		}
		return vo;
	}

	// 返回xml对象
	private static Document getXml(String configFileName) {
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(configFileName);
			doc.normalize();
			return doc;
		} catch (Exception e) {
			System.err.println("读取配置文件错误:" + e.toString());
			System.err.println("文件路径:" + configFileName);
		}
		return null;
	}

	public static void printReq(HttpServletRequest request) {
		System.err.println("开始打印查询参数["
				+ DateTimeUtil.getDateString(null, "yyyy-MM-dd hh:mm:ss")
				+ "]-------------------------------------");
		for (Enumeration em = request.getParameterNames(); em.hasMoreElements();) {
			String key = (String) em.nextElement();
			System.err.println(key + ":" + request.getParameter(key));
		}
		System.err
				.println("-------------------------------------------------------------------------\n\n");
	}

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) {
		// TODO 自动生成方法存根
		Instance s = new Instance();
		// System.out.println((String)Instance.getTreeHql("accountObjectTree_search"));
		TreeParaVo vo = (TreeParaVo) Instance.getTreeHql("accountObjectTree");
		System.out.println(vo.getFlag());
		System.out.println(vo.getSql());
		System.out.println(vo.getSearch_sql());
	}
}
